﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataModel
{
    public class SPNameConverter
    {
        /// <summary>
        /// Maps special characters to their SharePoint representation
        /// </summary>
        private static Dictionary<char, String> displayToInternal = new Dictionary<char, string>()
            {
                {' ', "_x0020_"},
                {'~', "_x007E_"},
                {'!', "_x0021_"},
                {'@', "_x0040_"},
                {'#', "_x0023_"},
                {'$', "_x0024_"},
                {'%', "_x0025_"},
                {'^', "_x005E_"},
                {'&', "_x0026_"},
                {'*', "_x002A_"},
                {'(', "_x0028_"},
                {')', "_x0029_"},
                {'+', "_x002B_"},
                {'-', "_x002D_"},
                {'=', "_x003D_"},
                {'{', "_x007B_"},
                {'}', "_x007D_"},
                {':', "_x003A_"},
                {'"', "_x0022_"},
                {'|', "_x007C_"},
                {';', "_x003B_"},
                {'\'', "_x0027_"},
                {'\\', "_x005C_"},
                {'<', "_x003C_"},
                {'>', "_x003E_"},
                {'?', "_x003F_"},
                {',', "_x002C_"},
                {'.', "_x002E_"},
                {'/', "_x002F_"},
                {'`', "_x0060_"}
            };

        /// <summary>
        /// Converts from display.
        /// </summary>
        /// <param name="msg">The display name.</param>
        /// <returns>The internal name</returns>
        public static String convertFromDisplay(String msg)
        {
            char[] charrArr = msg.ToCharArray();
            StringBuilder sb = new StringBuilder();

            foreach (Char c in charrArr)
            {
                if (displayToInternal.ContainsKey(c))
                {
                    sb.Append(displayToInternal[c]);
                }
                else
                {
                    sb.Append(c);
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// Converts from internal.
        /// </summary>
        /// <param name="msg">The internal name.</param>
        /// <returns>The display name</returns>
        public static String convertFromInternal(String msg)
        {
            foreach (KeyValuePair<char, string> x in displayToInternal)
            {
                msg.Replace(x.Value, x.Key.ToString());
            }

            return msg;
        }
    }
}
